<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="250px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/dataset-logo.svg" alt="logo" />
            <div class="title">YDS 数据集网站</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          style="font-size: 18px"
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="visibleRoutes"
          @click="doMenuClick"
        />
      </a-col>

      <!--      用户信息展示栏-->
      <a-col flex="180px">
        <div class="user-login-status">
          <div v-if="loginUserStore?.loginUser?.id">
            <a-dropdown>
              <a-space>
                <a-avatar
                  :src="COS_ENUM.COS_HOST + loginUserStore.loginUser.userAvatar + '?' + Date.now()"
                  :size="45"
                />
                <div class="ellipsis">
                  {{ loginUserStore.loginUser.userName ?? '无名' }}
                </div>
              </a-space>

              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link to="/user/center">
                      <UserOutlined />
                      我的主页
                    </router-link>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
// 未经过滤的菜单项
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, UserOutlined } from '@ant-design/icons-vue'
import router from '@/router'
import { type MenuProps, message } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingGet } from '@/api/userController.ts'
import checkAccess from '@/access/checkAccess.ts'
import { routes } from '@/router/routes.ts'
import SHOW_ENUM from '@/enums/showEnum.ts'
import cosEnum from '@/enums/CosEnum.ts'
import COS_ENUM from '@/enums/CosEnum.ts'

const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    // icon: () => h(HomeOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    // icon: () => h(HomeOutlined),
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    // icon: () => h(HomeOutlined),
    label: '空间管理',
    title: '空间管理',
  },
]

const loginUserStore = useLoginUserStore()

//展示在菜单的路由数组
//采用计算属性为了当用户属性发生变更时即使更新
const visibleRoutes = computed(() => {
  return routes
    .filter((item, index) => {
      if (item.meta?.hideInMenu) {
        return false
      }
      if (item.meta?.show !== SHOW_ENUM.HEAD_SHOW) {
        return false
      }
      // 根据权限过滤菜单
      return checkAccess(loginUserStore.loginUser, item?.meta?.access as string)
    })
    .map((item) => ({
      key: item.path,
      icon: item.meta?.icon ? () => h(item.meta?.icon, { style: { fontSize: '20px' } }) : null,
      label: item.name,
      title: item.name,
    }))
})

/**
 * 退出登录
 */
const doLogout = async () => {
  const res = await userLogoutUsingGet()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败' + res.data.message)
  }
}

// const filterMenus = (menus = [] as MenuProps['items']) => {
//   return menus?.filter((menu) => {
//     // 管理员才能看到admin开头的菜单
//     if (menu?.key?.startsWith('/admin')) {
//       const loginUser = loginUserStore.loginUser
//       if (!loginUser || loginUser.userRole !== 'admin') {
//         return false
//       }
//     }
//     return true
//   })
// }

// // 展示菜单的路由数组
// const items = computed(() => filterMenus(originItems))

// 当前要高亮的菜单项
const current = ref<string[]>([])
// 监听路由变化
router.afterEach((to, from, next) => {
  current.value = [to.path]
})

// 菜单点击事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.logo {
  height: 48px;
}

.title {
  color: black;
  font-size: 22px;
  margin-left: 16px;
}

.ellipsis {
  font-size: 18px;
  white-space: nowrap; /* 防止文本换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 显示省略号 */
  max-width: 120px; /* 设置最大宽度 */
}
</style>
